package com.example.administrator.tenyuanpackage.speacial.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.tenyuanpackage.R;
import com.example.administrator.tenyuanpackage.speacial.bean.TeMaiBean;
import com.example.administrator.tenyuanpackage.speacial.util.URLConfigs;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yq on 2017/2/27.
 */

public class TeMaiRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


   public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    private OnItemClickListener mListener;

    //设置专门的方法,处理点击事件
   public   void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    private Context context;
    private ArrayList<TeMaiBean.RowsBean> mList;

    public TeMaiRecyclerViewAdapter(Context context, ArrayList<TeMaiBean.RowsBean> mList) {
        this.context = context;
        this.mList = mList;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_speacial_recyclerview, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            TeMaiBean.RowsBean rowsBean = mList.get(position);
            Glide.with(context).load(URLConfigs.URL_Home + rowsBean.getImgUrlSml()).into(((MyViewHolder) holder).ivLogo);
            ((MyViewHolder) holder).tvName.setText(rowsBean.getName());
            //打折数据保留一位小数
            double discount = ((int) (rowsBean.getDisCount() * 10)) / 10.0;
            ((MyViewHolder) holder).tvDiscount.setText(discount + "折起");
            //将结束时间转换为毫秒
            String dateStr = rowsBean.getEndDateStr();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

            try {
                long millionSeconds = sdf.parse(dateStr).getTime();
                long current = System.currentTimeMillis();
                long days = (millionSeconds - current) / 1000 / 60 / 60 / 24;
                ((MyViewHolder) holder).tvLastDays.setText("剩" + days + "天");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //下边三个图片的数据
            String json = rowsBean.getProductInfo();
            ArrayList<TeMaiBean.ProductInfoBe> mListProductInfos = parseJsonToProductInfoBean(json);
            Glide.with(context).load(mListProductInfos.get(0).getProductImg()).into(((MyViewHolder) holder).ivGoods0);
            Glide.with(context).load(mListProductInfos.get(1).getProductImg()).into(((MyViewHolder) holder).ivGoods1);
            Glide.with(context).load(mListProductInfos.get(2).getProductImg()).into(((MyViewHolder) holder).ivGoods2);

            //设置三个图片中价格
            ((MyViewHolder) holder).tvPrice0.setText("￥" + mListProductInfos.get(0).getNewPrice());
            double discount0 = ((int) (mListProductInfos.get(0).getDisCount() * 10)) / 10.0;
            ((MyViewHolder) holder).tvDiscountItem0.setText(discount0 + "折");
            ((MyViewHolder) holder).tvPrice1.setText("￥" + mListProductInfos.get(1).getNewPrice());
            double discount1 = ((int) (mListProductInfos.get(1).getDisCount() * 10)) / 10.0;
            ((MyViewHolder) holder).tvDiscountItem1.setText(discount1 + "折");
            ((MyViewHolder) holder).tvPrice2.setText("￥" + mListProductInfos.get(2).getNewPrice());
            double discount2 = ((int) (mListProductInfos.get(2).getDisCount() * 10)) / 10.0;
            ((MyViewHolder) holder).tvDiscountItem2.setText(discount2 + "折");


        }


    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private ImageView ivLogo;
        private TextView tvName, tvDiscount, tvLastDays;
        private ImageView ivGoods0, ivGoods1, ivGoods2;
        private TextView tvPrice0, tvDiscountItem0, tvPrice1, tvDiscountItem1, tvPrice2, tvDiscountItem2;

        public MyViewHolder(View itemView) {
            super(itemView);

            ivLogo = (ImageView) itemView.findViewById(R.id.iv_logo_goods);
            tvName = (TextView) itemView.findViewById(R.id.tv_name_goods);
            tvDiscount = (TextView) itemView.findViewById(R.id.tv_discount_goods);
            tvLastDays = (TextView) itemView.findViewById(R.id.tv_last_days);

            ivGoods0 = (ImageView) itemView.findViewById(R.id.iv_goods0);
            ivGoods1 = (ImageView) itemView.findViewById(R.id.iv_goods1);
            ivGoods2 = (ImageView) itemView.findViewById(R.id.iv_goods2);
            tvPrice0 = (TextView) itemView.findViewById(R.id.tv_price0);
            tvDiscountItem0 = (TextView) itemView.findViewById(R.id.tv_discount0);
            tvPrice1 = (TextView) itemView.findViewById(R.id.tv_price1);
            tvDiscountItem1 = (TextView) itemView.findViewById(R.id.tv_discount1);
            tvPrice2 = (TextView) itemView.findViewById(R.id.tv_price2);
            tvDiscountItem2 = (TextView) itemView.findViewById(R.id.tv_discount2);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            //进行接口回调.
            //得到适配器中某个条目的位置
            int position = getAdapterPosition();
            //得到布局中某个条目的位置
            //int layoutPosition = getLayoutPosition();
            if (mListener != null) {
                mListener.onItemClick(v, position);
            }
        }
    }

    public ArrayList<TeMaiBean.ProductInfoBe> parseJsonToProductInfoBean(String json) {
        Gson gson = new Gson();
        return gson.fromJson(json, new TypeToken<List<TeMaiBean.ProductInfoBe>>() {
        }.getType());
    }
}
